<script>
import { checkIdCard } from '@/utils/idcard'

export default {
  name: 'VipAuth',
  data () {
    return {
      formData: {
        email: '',
        idcard: '',
        idcard1: '',
        idcard2: '',
        nickname: '',
        userinfoId: '',
        username: ''
      },
      rules: {
        nickname: [
          { required: true, message: '昵称不能为空' }
        ],
        email: [
          { required: true, message: '邮箱不能为空' },
          { pattern: /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/, message: '邮箱格式错误' }
        ],
        idcard: [
          { required: true, message: '身份证号不能为空' },
          {
            validator: (rule, value, callback) => {
              if (checkIdCard(value)) {
                callback()
              } else {
                callback(new Error('身证号码格式错误'))
              }
            }
          }
        ],
        idcard1: [
          { required: true, message: '请上传身份证正面' }
        ],
        idcard2: [
          { required: true, message: '请上传身份证反面' }
        ]
      },
      basePath: process.env.VUE_APP_BASE_PATH,
      show: false,
      uploadPth: process.env.VUE_APP_BASE_PATH + 'upload',
      headers: { token: this.$cookies.get('token') },
      idcard1Src: '',
      idcard2Src: ''
    }
  },
  methods: {
    init (id) {
      if (id) {
        this.$axios.get(`vip/info/${id}`).then(data => {
          this.formData = { ...data }
          this.idcard1Src = data.idcard1 ? this.basePath + data.idcard1 : ''
          this.idcard2Src = data.idcard2 ? this.basePath + data.idcard2 : ''
        }).catch(e => e)
      }
      this.show = true
    },
    submit () {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.$axios.post('vip/auth', this.formData).then(() => {
            this.$message.success('操作成功！')
            this.show = false
          }).catch(e => e)
        }
      })
    },
    beforeUpload (file) {
      const isJPG = file.type === 'image/png' || file.type === 'image/jpeg'
      const isLt10M = file.size / 1024 / 1024 < 10

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt10M) {
        this.$message.error('上传头像图片大小不能超过 10MB!')
      }
      return isJPG && isLt10M
    },
    onSuccess1 (res) {
      if (res.code === 200) {
        this.formData.idcard1 = res.data.filename
        this.idcard1Src = this.basePath + res.data.filename
      } else {
        this.$message.error(res.msg)
      }
    },
    onSuccess2 (res) {
      if (res.code === 200) {
        this.formData.idcard2 = res.data.filename
        this.idcard2Src = this.basePath + res.data.filename
      } else {
        this.$message.error(res.msg)
      }
    }
  }
}
</script>

<template>
<el-dialog title="实名认证" :visible.sync="show" width="40%">
  <el-form :model="formData" :rules="rules" ref="formRef" label-width="120px">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="formData.username" disabled/>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="formData.nickname" placeholder="请输入昵称" clearable/>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="formData.email" placeholder="请输入邮箱地址" clearable/>
    </el-form-item>
    <el-form-item label="身份证号" prop="idcard">
      <el-input v-model="formData.idcard" placeholder="请输入身份证号" clearable/>
    </el-form-item>
    <el-form-item label="身份证正面" prop="idcard1">
      <el-upload
        class="avatar-uploader"
        :action="uploadPth"
        :headers="headers"
        :show-file-list="false"
        :data="{type:'idcard'}"
        :on-success="onSuccess1"
        :before-upload="beforeUpload"
      >
        <img v-if="idcard1Src" :src="idcard1Src" class="avatar" alt="图片异常">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item label="身份证反面" prop="idcard2">
      <el-upload
        class="avatar-uploader"
        :action="uploadPth"
        :headers="headers"
        :show-file-list="false"
        :data="{type:'idcard'}"
        :on-success="onSuccess2"
        :before-upload="beforeUpload"
      >
        <img v-if="idcard2Src" :src="idcard2Src" class="avatar" alt="图片异常">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item>
      <el-button @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</el-dialog>
</template>

<style scoped lang="scss">

</style>
